package com.cskaoyan.service;

import com.cskaoyan.bean.MarketComment;
import com.cskaoyan.bean.MarketCommentExample;
import com.cskaoyan.bean.MarketGoodsExample;
import com.cskaoyan.bean.common.BasePageInfo;
import com.cskaoyan.bean.common.CommonData;
import com.cskaoyan.mapper.MarketCommentMapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @description:
 * @author: Tao
 * @time: 2022/12/29 20:37
 */
@Service
public class CommentServiceImpl implements CommentService {
    @Autowired
    MarketCommentMapper commentMapper;

    /**
     * @param info
     * @param id
     * @param goodsId
     * @return 商品的评论表
     */
    @Override
    public CommonData<MarketComment> getList(BasePageInfo info, Integer id, Integer goodsId) {
        //开启分页
        PageHelper.startPage(info.getPage(), info.getLimit());
        //实例化对象
        MarketCommentExample example = new MarketCommentExample();
        //先排序
        example.setOrderByClause(info.getSort() + " " + info.getOrder());
        //增加条件查询
        MarketCommentExample.Criteria criteria = example.createCriteria();

        // 查询逻辑存在,表中deleted字段值为1是删除状态
        criteria.andDeletedEqualTo(false);
        //对数据进行判空，如果数据是null则此sql语句不执行
        if (id != null) {
            criteria.andUserIdEqualTo(id);
        }
        if (goodsId != null) {
            criteria.andValueIdEqualTo(goodsId);
        }
        //获取商品表
        List<MarketComment> list = commentMapper.selectByExample(example);
        //封装
        PageInfo<MarketComment> pageInfo = new PageInfo<>(list);
        //进一步封装
        return CommonData.data(pageInfo);
    }

    /**
     * 删除评论
     * @param marketComment
     */
    @Override
    public void delete(MarketComment marketComment) {

        // 获得 要删除的问题的id
        Integer id = marketComment.getId();
        // 获得 要删除的问题的逻辑删除标志
        Boolean deleted = marketComment.getDeleted();
        // 通过id 更新 删除标记
        MarketComment issueDelete = new MarketComment();
        issueDelete.setId(id);
        // 删除标记取反
        issueDelete.setDeleted(!deleted);
        commentMapper.updateByPrimaryKeySelective(issueDelete);
    }
}
